<template>
  <el-card>
    <el-breadcrumb separator="/">
      <el-breadcrumb-item>
        <a href="/home" onclick="return false" @click="back">首页</a>
      </el-breadcrumb-item>
      <el-breadcrumb-item>系统管理</el-breadcrumb-item>
      <el-breadcrumb-item>用户管理</el-breadcrumb-item>
    </el-breadcrumb>
    <el-button type="primary" @click="dialogFormVisible = true">添加用户</el-button>
    <el-table
      ref="multipleTable"
      :data="tableData"
      stripe
      style="width: 100%">
      <el-table-column
        type="index"
        width="180"
        align="center"
      >
      </el-table-column>
      <el-table-column
        label="注册日期"
        align="center">
        <template slot-scope="scope">
          {{ scope.row.regDate | dataFormat }}
        </template>
      </el-table-column>
      <el-table-column
        prop="username"
        label="用户名"
        align="center">
      </el-table-column>
      <el-table-column
        label="性别"
        align="center">
        <template slot-scope="scope">
          <div v-if="scope.row.gender === -1">
            保密
          </div>
          <div v-if="scope.row.gender === 0">
            男
          </div>
          <div v-if="scope.row.gender === 1">
            女
          </div>
        </template>
      </el-table-column>
      <el-table-column
        label="用户身份"
        align="center">
        <template slot-scope="scope">
          {{ scope.row.auth ? '用户' : '超级管理员' }}
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center">
        <template slot-scope="scope">
          <el-button
            type="primary"
            size="small"
            icon="el-icon-edit"
            @click="handleDialog(scope.row)"
          >编辑</el-button>
          <el-button
            type="danger"
            size="small"
            icon="el-icon-delete"
            @click="removeUser(scope.row._id)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @current-change="handlePagenumChange"
      background
      layout="prev, pager, next"
      :total="this.total"
      :page-size="9"
      :hide-on-single-page="false">
    </el-pagination>
    <el-dialog width="40%" title="添加用户" :visible.sync="dialogFormVisible">
      <el-form :model="addForm">
        <el-form-item label="用户名：" label-width="90px">
          <el-input v-model="addForm.username" placeholder="请输入用户名"></el-input>
        </el-form-item>
        <el-form-item label="密码：" label-width="90px">
          <el-input
            placeholder="请输入密码"
            v-model="addForm.password">
          </el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="addUser">确 定</el-button>
      </div>
    </el-dialog>
    <el-dialog title="用户编辑" :visible.sync="dialogVisible">
      <el-form :model="userForm" label-width="90px">
        <el-form-item label="用户名：">
          <el-input v-model="userForm.username"></el-input>
        </el-form-item>
        <el-form-item label="密码：">
          <el-input
            placeholder="请输入密码"
            v-model="userForm.password">
          </el-input>
        </el-form-item>
        <el-form-item label="性别：">
          <el-select v-model="userForm.gender">
            <el-option
              v-for="item in genderOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="用户身份：">
          <el-select v-model="userForm.auth">
            <el-option
              v-for="item in authOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="changeById">确 定</el-button>
      </div>
    </el-dialog>
  </el-card>
</template>

<script>
export default {
  name: 'Users',
  data () {
    return {
      tableData: [],
      total: 0,
      pageNum: 1,
      addForm: {
        username: '',
        password: ''
      },
      userForm: {},
      genderOptions: [
        {
          value: -1,
          label: '保密'
        },
        {
          value: 0,
          label: '男'
        },
        {
          value: 1,
          label: '女'
        }
      ],
      authOptions: [
        {
          value: 0,
          label: '超级管理员'
        },
        {
          value: 1,
          label: '用户'
        }
      ],
      dialogFormVisible: false,
      dialogVisible: false
    }
  },
  created () {
    this.getUserList()
  },
  methods: {
    clear () {
      this.addForm.username = ''
      this.addForm.password = ''
    },
    back () {
      this.$router.push('/home')
      this.$store.state.activePath = '/home'
    },
    async getUserList () {
      const { data: res } = await this.$http.get(`/userlist?pagenum=${this.pageNum}`)
      if (res.status !== 200) return this.$message.error('获取用户列表失败')
      this.tableData = res.data
      this.total = res.total
      if (res.data.length === 0 && this.pageNum !== 1) {
        this.pageNum -= 1
        this.getUserList()
      }
    },
    handlePagenumChange (val) {
      this.pageNum = val
      this.getUserList()
    },
    async addUser () {
      const { data: res } = await this.$http.post('/user/new', this.addForm)
      if (res.status === 399) return this.$message.error('用户名重复')
      if (res.status !== 200 && res.status !== 399) return this.$message.error('添加用户失败')
      this.dialogFormVisible = false
      this.$message.success('添加用户成功')
      this.getUserList()
      this.clear()
    },
    handleDialog (data) {
      this.dialogVisible = true
      this.userForm = data
    },
    async changeById () {
      const { data: res } = await this.$http.post('/user', this.userForm)
      if (res.status !== 200) return this.$message.error('修改用户信息失败')
      this.dialogVisible = false
      this.getUserList()
    },
    async removeUser (id) {
      const confirmResult = await this.$confirm('此操作将永久删除该用户, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err => err)
      if (confirmResult !== 'confirm') {
        return this.$message.info('已取消删除')
      }
      const { data: res } = await this.$http.delete(`/user?id=${id}`)
      if (res.status !== 200) {
        return this.$message.error('删除用户失败！')
      }
      this.$message.success('删除用户成功！')
      this.getUserList()
    }
  }
}
</script>

<style scoped>
.el-input{
  width: 70%;
}
</style>
